package com.hly.musicbackground.model.boots.rest;

import com.hly.musicbackground.Enum.UserLogEnum;
import com.hly.musicbackground.Utils.RequestContextUtils;
import com.hly.musicbackground.model.boots.VO.FictionShelfListVO;
import com.hly.musicbackground.model.boots.params.FictionShelfDelParams;
import com.hly.musicbackground.model.boots.params.FictionShelfProgressParams;
import com.hly.musicbackground.model.boots.service.FictionService;
import com.hly.musicbackground.model.boots.service.FictionShelfService;
import com.hly.musicbackground.model.user.service.UserLogService;
import com.hly.musicbackground.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 小说书架
 * */
@RestController
@RequestMapping("/api/fictionShelf/")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "小说书架")
public class FictionShelfController {

    private final FictionShelfService fictionShelfService;
    private final FictionService fictionService;
    private final UserLogService userLogService;

    /**
     * 获取书架小说
     * */
    @GetMapping("/list")
    @ApiOperation(value = "获取书架小说")
    public Result<FictionShelfListVO> list(
            @RequestParam(defaultValue = "all") String type,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int limit){

        int userId = RequestContextUtils.getUser().getUserId();

        FictionShelfListVO fictionShelfListVO = fictionService
                .getFictionShelfListVO(userId, type, page, limit);
        return Result.ok(fictionShelfListVO);
    }


    /**
     * 检查用户书架中的小说是否更新
     * */
    @PostMapping("/checkNew")
    @ApiOperation(value = "检查用户书架中的小说是否更新")
    public Result<Boolean> checkNew(){
        int userId = RequestContextUtils.getUser().getUserId();
        return Result.ok(fictionService.UserBookshelfInspectUpdateFiction(userId));
    }


    /**
     * 添加、修改到书架
     * */
    @PostMapping("/addOrupdate")
    @ApiOperation(value = "添加/修改到书架")
    public Result add(@RequestBody FictionShelfProgressParams params){
        int userId = RequestContextUtils.getUser().getUserId();
        fictionShelfService.saveOrUpdateFictionShelfType(
                params.getFid(),
                userId,
                params.getType(),
                params.getFcId(),
                params.getNum());
        userLogService.addUserLogOrFail(params.getFcId(), UserLogEnum.READ_FICTION_CHAPTER, "");
        return Result.ok();
    }


    /**
     * 移除
     * @param params 小说id
     * */
    @DeleteMapping("/del")
    @ApiOperation(value = "移除")
    public Result del(@RequestBody FictionShelfDelParams params){
        int userId = RequestContextUtils.getUser().getUserId();
        fictionShelfService.deleteType(new String[]{params.getFid() + ""},
                userId, params.getType());
        return Result.ok();
    }

    /**
     * 批量移除
     * @param ids 小说id们，多个id用,分隔
     * */
    @DeleteMapping("/dels/{ids}")
    @ApiOperation(value = "批量移除")
    public Result deletes(@PathVariable("ids") String ids,
                                           @RequestBody String type){
        String[] split = ids.split(",");
        int userId = RequestContextUtils.getUser().getUserId();
        fictionShelfService.deleteType(split, userId, type);
        return Result.ok();
    }


}
